{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()\n",
    "#加载数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x22dc9850190>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAOLElEQVR4nO3dcYwc9XnG8efBnJ3G4MQ2gbjEBAg0QKhq6AkSnLYU0uAgVQYUCqhJTYMwIhASiSpF9I8gtZVoREKjqEU1xcSkhAQpUFCDEiw3CQolFgdysB0DdsAB21cbarWYEJuz7+0fN7QH3P7u2Nnd2eP9fqTV7s67M/NqfY9nd3+783NECMDb30FNNwCgNwg7kARhB5Ig7EAShB1I4uBe7mymZ8U7NLuXuwRS2atf6dXY54lqtcJue4mkr0maIemfI+LG0uPfodk63WfX2SWAgrWxpmWt7ZfxtmdI+gdJn5B0kqRLbJ/U7vYAdFed9+ynSdoSEc9ExKuSvi1paWfaAtBpdcJ+pKTnx93fVi17HdvLbQ/ZHhrRvhq7A1BHnbBP9CHAm757GxErImIwIgYHNKvG7gDUUSfs2yQtHHf/fZJ21GsHQLfUCfujko63fYztmZIulnR/Z9oC0GltD71FxH7bV0v6gcaG3lZGxMaOdQago2qN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Ek/hdrUC9l1r3UJAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(train_images[7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x22dc992b760>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAOhElEQVR4nO3dfYwc9X3H8c/HjjFgY2qH2Lg8lBTZbShNbHLFNEZtKhREUCRDEyqoRA2lMaowDy1NQaRSqBpFtAlJUWiJnGDFIAKJlFAsFbVxTyBEIJbP1DF2TDChhCfnHOqoPBX7fP72jxtXh7mZW+/MPhzf90s67e58d2a+Wvtzsze/2f05IgTg3W9arxsA0B2EHUiCsANJEHYgCcIOJPGebu7sCM+MIzWrm7sEUnlLb2hf7PVEtVpht32epNskTZf0jYi4per5R2qWlvmcOrsEUGFjDJbW2n4bb3u6pH+S9HFJp0m6xPZp7W4PQGfV+Zv9TEnPRMSzEbFP0n2SVjTTFoCm1Qn7CZJeGPf4xWLZ29heZXvI9tCI9tbYHYA66oR9opMA77j2NiLWRMRARAzM0MwauwNQR52wvyjppHGPT5T0cr12AHRKnbBvkrTI9vttHyHpYknrm2kLQNPaHnqLiP22V0v6d40Nva2NiO2NdQagUbXG2SPiQUkPNtQLgA7iclkgCcIOJEHYgSQIO5AEYQeSIOxAEoQdSIKwA0kQdiAJwg4kQdiBJAg7kARhB5Ig7EAShB1IgrADSRB2IAnCDiRB2I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II=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(train_images[27])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_images = train_images.reshape(60000, 28*28)\n",
    "test_images = test_images.reshape(10000, 28*28)\n",
    "#维度变换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_images = train_images / 255\n",
    "test_images = test_images/ 255\n",
    "#归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "70.34901960784313\n"
     ]
    }
   ],
   "source": [
    "distance = np.sum(np.abs(train_images[7]-train_images[27]))\n",
    "print(distance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_labels = np.array(pd.get_dummies(train_labels))\n",
    "test_labels = np.array(pd.get_dummies(test_labels))\n",
    "#独热编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 0, 0, 0, 0], dtype=uint8)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_labels[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "real = np.argmax(train_labels[0])\n",
    "print(real)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测: 7 真实: 7\n",
      "预测: 2 真实: 2\n",
      "预测: 1 真实: 1\n",
      "预测: 0 真实: 0\n",
      "预测: 4 真实: 4\n",
      "预测: 1 真实: 1\n",
      "预测: 4 真实: 4\n",
      "预测: 9 真实: 9\n",
      "预测: 6 真实: 5\n",
      "预测: 9 真实: 9\n",
      "预测: 0 真实: 0\n",
      "预测: 6 真实: 6\n",
      "预测: 9 真实: 9\n",
      "预测: 0 真实: 0\n",
      "预测: 1 真实: 1\n",
      "预测: 5 真实: 5\n",
      "预测: 9 真实: 9\n",
      "预测: 7 真实: 7\n",
      "预测: 3 真实: 3\n",
      "预测: 4 真实: 4\n",
      "预测: 9 真实: 9\n",
      "预测: 6 真实: 6\n",
      "预测: 6 真实: 6\n",
      "预测: 5 真实: 5\n",
      "预测: 9 真实: 4\n",
      "预测: 0 真实: 0\n",
      "预测: 7 真实: 7\n",
      "预测: 4 真实: 4\n",
      "预测: 0 真实: 0\n",
      "预测: 1 真实: 1\n",
      "预测: 3 真实: 3\n",
      "预测: 1 真实: 1\n",
      "预测: 3 真实: 3\n",
      "预测: 4 真实: 4\n",
      "预测: 7 真实: 7\n",
      "预测: 2 真实: 2\n",
      "预测: 7 真实: 7\n",
      "预测: 1 真实: 1\n",
      "预测: 2 真实: 2\n",
      "预测: 1 真实: 1\n",
      "预测: 1 真实: 1\n",
      "预测: 7 真实: 7\n",
      "预测: 4 真实: 4\n",
      "预测: 2 真实: 2\n",
      "预测: 3 真实: 3\n",
      "预测: 5 真实: 5\n",
      "预测: 1 真实: 1\n",
      "预测: 2 真实: 2\n",
      "预测: 4 真实: 4\n",
      "预测: 4 真实: 4\n",
      "准确率: 0.96\n"
     ]
    }
   ],
   "source": [
    "test = 50\n",
    "acc = 0\n",
    "\n",
    "for i in range(test):\n",
    "    test_data = test_images[i]\n",
    "    train_data = train_images[:20000, :]\n",
    "    #选择使用的训练集数量\n",
    "\n",
    "    distance =np.argmin(np.sum(np.abs(test_data-train_data),axis=1))\n",
    "    #计算L1距离\n",
    "\n",
    "    predict = np.argmax(train_labels[distance])\n",
    "    #取得距离最近图片的标签\n",
    "    real = np.argmax(test_labels[i])\n",
    "    #取得真实标签\n",
    "        \n",
    "    if predict == real:\n",
    "    #判断真实和预测\n",
    "        acc += 1\n",
    "        #准确数增加\n",
    "    print(\"预测:\", predict, \"真实:\", real)\n",
    "       \n",
    "print(\"准确率:\", acc/test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
